<?php
class Default_Model_Managers_Images extends Default_Model_Managers_EntityManagerAbstract
{
	public function resizeAndUpload($width, $height, $to_adress_name, $quality, $tempFile)
	{
		$img = imagecreatefromjpeg($tempFile);
		$size = getimagesize($tempFile);
		$x_ratio = $width / $size[0];
		$y_ratio = $height / $size[1];
		$ratio = min($x_ratio, $y_ratio);
		$use_x_ratio = ($x_ratio == $ratio);
		$new_width   = $use_x_ratio  ? $width  : floor($size[0] * $ratio);
		$new_height  = !$use_x_ratio ? $height : floor($size[1] * $ratio);
		$new_left    = $use_x_ratio  ? 0 : floor(($width - $new_width) / 2);
		$new_top     = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
		$idest = imagecreatetruecolor($new_width, $new_height);
		$rgb=0xFFFFFF;
		imagefill($idest, 0, 0, $rgb);
		imagecopyresampled($idest, $img, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
		imagejpeg($idest, $to_adress_name, $quality);
	}
}